package org.metasketch.simulator.perspective.validation;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;

import org.metasketch.demo.project.DEMOProject;
import org.metasketch.editor.Project;
import org.metasketch.editor.perspectives.ILayout;
import org.metasketch.editor.project.MultipleProject;
import org.metasketch.simulator.configuration.MainLoader;
import org.metasketch.simulator.configuration.SimulationPackage;
import org.metasketch.simulator.runtime.SimulatorEngine;
import org.metasketch.simulator.transformation.PetriNetSimulationPackage;
import org.metasketch.simulator.transformation.demo.Transformer;

public class ValidationLayout extends Group implements ILayout,
		EventHandler<MouseEvent> {

	private DEMOProject project;
	private Button transformButton;
	private Button simulateButton;

	public ValidationLayout() {

		transformButton = new Button("Transform to Petri Net");

		transformButton.setMaxWidth(320);
		transformButton.setMinWidth(320);
		transformButton.setPrefWidth(320);

		getChildren().add(transformButton);
		transformButton.setOnMouseClicked(this);

		simulateButton = new Button("Simulate in Petri Net");

		simulateButton.setMaxWidth(320);
		simulateButton.setMinWidth(320);
		simulateButton.setPrefWidth(320);

		getChildren().add(simulateButton);
		simulateButton.setOnMouseClicked(this);

	}

	@Override
	public void handle(MouseEvent arg0) {
		try {
			Object source = arg0.getSource();
			if (source == transformButton) {
				PetriNetSimulationPackage pack = Transformer.transform(project);

			} else if (source == simulateButton) {
				PetriNetSimulationPackage simulPackage = Transformer
						.transform(project);
				SimulatorEngine engine = new SimulatorEngine();

				MainLoader loader = new MainLoader();
				SimulationPackage simPack;

				simPack = loader.loadSimulationPackaged(project, simulPackage);

				engine.initialize(simulPackage, simPack);
				engine.startSimulation();
			}

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void setup(Project project) {
		this.project = ((MultipleProject) project).getDemoProject();
	}

	@Override
	public void apply(Object editor) {
		BorderPane pane = (BorderPane) editor;
		pane.setCenter(this);
	}

}
